﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;

namespace BasicFrameworkDemo.Utility
{
    using NHibernate;
    using NHibernate.Context;
    using NHibernate.Tool.hbm2ddl;

    public abstract class NHibernateUtilityBase
    {
        /// <summary>
        /// Gets or sets the session factory.
        /// </summary>
        /// <value>
        /// The session factory.
        /// </value>
        /// Creator:LiLei
        /// CreationDate:6/27/2012 11:58 AM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public ISessionFactory SessionFactory { get; private set; }

        public IStatelessSession StatelessSession { get; private set; }


        /// <summary>
        /// Configs this instance.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:6/27/2012 11:16 AM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public void Config()
        {
            var nConfig = NConfig();
            SessionFactory = nConfig.BuildSessionFactory();
        }

        /// <summary>
        /// Ns the config.
        /// </summary>
        /// <returns></returns>
        protected virtual Configuration NConfig()
        {
            var configPath = SystemConfig.NHibernateCfgPath ?? string.Empty;
            var filePath = string.Format("{0}/{1}", AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\', '/'),
                                         configPath.TrimStart('\\', '/'));
            var nConfig = new NHibernate.Cfg.Configuration().Configure(filePath);
            return nConfig;
        }

        /// <summary>
        /// Binds the session.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/22/2012 3:23 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public ISession GetSession()
        {
            return SessionFactory.OpenSession();
            //CallSessionContext.Bind(session);

        }

        /// <summary>
        /// Binds the stateless session.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/22/2012 3:23 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public IStatelessSession GetStatelessSession()
        {
            return SessionFactory.OpenStatelessSession();
        }

        /// <summary>
        /// Disposes the session.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/22/2012 3:23 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public void DisposeSession()
        {
            var session = CurrentSessionContext.Unbind(SessionFactory);
            session.Dispose();
        }

        /// <summary>
        /// Updates the db.
        /// </summary>
        public void UpdateDb()
        {
            var nconfig = NConfig();
            var schemaExport = new SchemaExport(nconfig);
            schemaExport.SetOutputFile("DBSql.script").Create(true, true);
        }
    }
}
